
using System;
using System.Data;
using System.Linq;
using System.Linq.Dynamic;
using System.Linq.Dynamic.Core;
using System.Linq.Expressions;
using System.Threading.Tasks;
using System.Collections.Generic;
using Microsoft.EntityFrameworkCore;

using Abp.UI;
using Abp.AutoMapper;
using Abp.Extensions;
using Abp.Authorization;
using Abp.Domain.Repositories;
using Abp.Application.Services.Dto;
using Abp.Linq.Extensions;


using JYJ.AiYard.StationManagement.TbStations;
using JYJ.AiYard.StationManagement.TbStations.Dtos;
using JYJ.AiYard.StationManagement.TbStations.DomainService;
using JYJ.AiYard.StationManagement.TbYardIssuedCards;
using JYJ.AiYard.Database;

namespace JYJ.AiYard.StationManagement.TbStations
{
    /// <summary>
    /// TbStation应用层服务的接口实现方法  
    ///</summary>
    [AbpAuthorize]
    public class TbStationAppService : AiYardAppServiceBase, ITbStationAppService
    {
        private readonly IRepository<TbStation, long> _entityRepository;

        private readonly ITbStationManager _entityManager;

        private readonly IAiYardDbTableIssuedCard _aiYardDbTableIssuedCard;


        /// <summary>
        /// 构造函数 
        ///</summary>
        public TbStationAppService(
        IRepository<TbStation, long> entityRepository
        ,ITbStationManager entityManager
        ,IAiYardDbTableIssuedCard aiYardDbTableIssuedCard
        )
        {
            _entityRepository = entityRepository; 
            _entityManager=entityManager;
            _aiYardDbTableIssuedCard = aiYardDbTableIssuedCard;
        }


        /// <summary>
        /// 获取TbStation的分页列表信息
        ///</summary>
        /// <param name="input"></param>
        /// <returns></returns>
		 
        public async Task<PagedResultDto<TbStationListDto>> GetPaged(GetTbStationsInput input)
		{

		    var query = _entityRepository.GetAll().Where(o => GetCurrenUserOrganizationAndChildrenUnit().Contains(o.Station_Id));
			// TODO:根据传入的参数添加过滤条件
            

			var count = await query.CountAsync();

			var entityList = await query
					.OrderBy(input.Sorting).AsNoTracking()
					.PageBy(input)
					.ToListAsync();
             
			var entityListDtos =entityList.MapTo<List<TbStationListDto>>();

			return new PagedResultDto<TbStationListDto>(count,entityListDtos);
		}


		/// <summary>
		/// 通过指定id获取TbStationListDto信息
		/// </summary>
		 
		public async Task<TbStationListDto> GetById(EntityDto<long> input)
		{
			var entity = await _entityRepository.GetAsync(input.Id);

		    return entity.MapTo<TbStationListDto>();
		}



        /// <summary>
        /// 通过组织ID获取TbStationListDto信息
        /// </summary>
        public async Task<TbStationListDto> GetByStationId(EntityDto<long> input)
        {
            var entity = await _entityRepository.SingleAsync(o => input.Id == o.Station_Id);
            return entity.MapTo<TbStationListDto>();
        }


        /// <summary>
        /// 通过用户ID获取场所及子项
        /// </summary>
        public async Task<List<TbStationTreeDto>> GetByStationUserId()
        {
            var orgId = _aiYardDbTableIssuedCard.GetCurrenUserOrganizationAndChildrenUnitResultDto((long)AbpSession.UserId);
            
            //var entity = _entityRepository.GetAll().Where(o => orgId.Contains(o.Station_Id));
            //var allTbDepart = entity.MapTo<List<TbStationListDto>>();
            return orgId.MapTo<List<TbStationTreeDto>>(); ;
        }


        /// <summary>
        /// 获取编辑 TbStation
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>

        public async Task<GetTbStationForEditOutput> GetForEdit(NullableIdDto<long> input)
		{
			var output = new GetTbStationForEditOutput();
             TbStationEditDto editDto;

			if (input.Id.HasValue)
			{
				var entity = await _entityRepository.GetAsync(input.Id.Value);

				editDto = entity.MapTo<TbStationEditDto>();

				//tbStationEditDto = ObjectMapper.Map<List<tbStationEditDto>>(entity);
			}
			else
			{
				editDto = new TbStationEditDto();
			}

			output.TbStation = editDto;
			return output;
		}


		/// <summary>
		/// 添加或者修改TbStation的公共方法
		/// </summary>
		/// <param name="input"></param>
		/// <returns></returns>
		
		public async Task CreateOrUpdate(CreateOrUpdateTbStationInput input)
		{

			if (input.TbStation.Id.HasValue)
			{
				await Update(input.TbStation);
			}
			else
			{
				await Create(input.TbStation);
			}
		}


		/// <summary>
		/// 新增TbStation
		/// </summary>
		
		protected virtual async Task<TbStationEditDto> Create(TbStationEditDto input)
		{
			//TODO:新增前的逻辑判断，是否允许新增

            // var entity = ObjectMapper.Map <TbStation>(input);
            var entity=input.MapTo<TbStation>();
			

			entity = await _entityRepository.InsertAsync(entity);
			return entity.MapTo<TbStationEditDto>();
		}

		/// <summary>
		/// 编辑TbStation
		/// </summary>
		
		protected virtual async Task Update(TbStationEditDto input)
		{
			//TODO:更新前的逻辑判断，是否允许更新

			var entity = await _entityRepository.GetAsync(input.Id.Value);
			input.MapTo(entity);

			// ObjectMapper.Map(input, entity);
		    await _entityRepository.UpdateAsync(entity);
		}



		/// <summary>
		/// 删除TbStation信息的方法
		/// </summary>
		/// <param name="input"></param>
		/// <returns></returns>
		
		public async Task Delete(EntityDto<long> input)
		{
			//TODO:删除前的逻辑判断，是否允许删除
			await _entityRepository.DeleteAsync(input.Id);
		}



		/// <summary>
		/// 批量删除TbStation的方法
		/// </summary>
		
		public async Task BatchDelete(List<long> input)
		{
			// TODO:批量删除前的逻辑判断，是否允许删除
			await _entityRepository.DeleteAsync(s => input.Contains(s.Id));
		}


		///// <summary>
		///// 导出TbStation为excel表,等待开发。
		///// </summary>
		///// <returns></returns>
		//public async Task<FileDto> GetToExcel()
		//{
		//	var users = await UserManager.Users.ToListAsync();
		//	var userListDtos = ObjectMapper.Map<List<UserListDto>>(users);
		//	await FillRoleNames(userListDtos);
		//	return _userListExcelExporter.ExportToFile(userListDtos);
		//}

    }
}


